//
//  JXLStatusRetweetView.swift
//  MicroBlog
//
//  Created by 贾晓磊 on 16/9/24.
//  Copyright © 2016年 贾晓磊. All rights reserved.
//

import UIKit

//转发微博视图
class JXLStatusRetweetView: UIView {
    
    //纪录转发微博底部约束
    var retweetViewBottomConstraint: Constraint?
    
    var statusViewModel:JXLStatusViewModel? {
        didSet{
            contentLabel.text = statusViewModel?.retweetContent
            //            //绑定转发微博配图
            //           pictureView.picUrls = statusViewModel?.status?.retweeted_status?.pic_urls
            
            //卸载上次约束
            retweetViewBottomConstraint?.uninstall()
            
            if let picUrls = statusViewModel?.status?.retweeted_status?.pic_urls, picUrls.count > 0 {
                //  表示有配图, 显示配图,更新约束,绑定数据
                pictureView.isHidden = false
                
                self.snp_updateConstraints(closure: { (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(JXLHomeTableViewCellMargin).constraint
                    
                    pictureView.picUrls = picUrls
                })
            } else {
                //显示配图
                pictureView.isHidden = true
                
                self.snp_updateConstraints(closure: { (make) in
                    //  表示没有配图,更新约束
                    retweetViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(JXLHomeTableViewCellMargin).constraint
                })
            }
        }
    }
    
    //懒加载配图collectionView
   private lazy var pictureView: JXLStatusPictureView = {
        let view = JXLStatusPictureView()
        
        view.backgroundColor = self.backgroundColor
        return view
    }()
    
    //懒加载内容lebal
    private lazy var contentLabel:UILabel = {
        let label = UILabel(fontSize: 13, textColor: UIColor.lightGray)
        //设置多行显示
        label.numberOfLines = 0
        
        label.text = "hello,world"
        
        return label
    }()
    
    
    //重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置转发视图子控件
    private func setupUI() {
        
        backgroundColor = UIColor(white: 0.95, alpha: 1)

        
        //添加子控件
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //布局contentLabel
        contentLabel.snp_makeConstraints { (make) in
            make.top.leading.equalTo(self).offset(JXLHomeTableViewCellMargin)
            make.width.equalTo(ScreenWidth - 2 * JXLHomeTableViewCellMargin)
        }
        
        //布局pictureView
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(JXLHomeTableViewCellMargin)
            
            make.leading.equalTo(contentLabel)
            
            //            //暂时写死，以后根据图片张数确定大小
            //            make.size.equalTo(CGSize(width: 100, height: 100))
            
        }
        
        //  关键约束 -> 指定当前视图的底部约束=转发微博内容底部的约束+间距
        self.snp_makeConstraints { (make) in
            retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(JXLHomeTableViewCellMargin).constraint
        }
    }
}
